﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;

namespace RedisSessionStore.Redis
{
	internal class SingleRedisPool
	{
		private static volatile PooledRedisClientManager _Pool;
		private static Object _PoolLocker = new Object();

		/// <summary>
		/// Thread safe way to get a client to redis.
		/// </summary>
		/// <param name="server">Host where redis is running. Note: Pool is created only on first call, so passing different server next time won't have any effect.</param>
		/// <returns>Redis client</returns>
		public static IRedisClient GetClient(string server)
		{
			if (_Pool == null)
			{
				lock (_PoolLocker)
				{
					if (_Pool == null)
					{
						_Pool = new PooledRedisClientManager(server);
					}
				}
			}

			return _Pool.GetClient();
		}
	}
}
